﻿package app.view.components.sections.popup 
{
	import app.model.RemoteProxy;
	import app.view.components.buttons.SelectableButton;
	import app.view.components.header.HeadMarkerBase;
	import app.view.components.sections.LabeledSectionBase;
	import app.view.components.sections.Section;
	import app.view.events.UIEvent;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	/**
	 * 1.1.2.3 POPUP - УКАЗАТЕЛЬ ОБЪЕКТОВ
	 * firsoff maxim, snzflash.com, tel: +79222345364, skype: maxim.firsoff
	 */
	public class PopupExpoObjectsMBase extends LabeledSectionBase
	{
		// app.view.components.sections.popup.PopupExpoObjectsMBase
		
		public var closeBtn:Sprite;
		public var bgd:Sprite;
		
		public var btnnumber_01:SelectableButton;
		public var btnnumber_02:SelectableButton;
		public var btnnumber_03:SelectableButton;
		public var btnnumber_04:SelectableButton;
		public var btnnumber_05:SelectableButton;
		public var btnnumber_06:SelectableButton;
		public var btnnumber_07:SelectableButton;
		public var btnnumber_08:SelectableButton;
		public var btnnumber_09:SelectableButton;
		public var btnnumber_10:SelectableButton;
		public var btnnumber_11:SelectableButton;
		public var btnnumber_12:SelectableButton;
		public var btnnumber_13:SelectableButton;
		public var btnnumber_14:SelectableButton;
		public var btnnumber_15:SelectableButton;
		public var btnnumber_16:SelectableButton;
		public var btnnumber_17:SelectableButton;
		public var btnnumber_18:SelectableButton;
		
		private var _btns:/*SelectableButton*/Array;
		private var _dataItems:Array;
		
		public function PopupExpoObjectsMBase() 
		{
			super();
		}
		
		override protected function init():void
		{
			super.init();
			
			_btns = new Array();
			
			_btns = [btnnumber_01, btnnumber_02, btnnumber_03, btnnumber_04, btnnumber_05, btnnumber_06,
					btnnumber_07, btnnumber_08, btnnumber_09, btnnumber_10, btnnumber_11, btnnumber_12,
					btnnumber_13,btnnumber_14,btnnumber_15,btnnumber_16,btnnumber_17,btnnumber_18];
			
			for (var i:int = 0; i < _btns.length; i++) 
			{
				_btns[i].visible = false;
			}
		}
		
		private function setEvents():void
		{
			closeBtn.addEventListener(MouseEvent.CLICK, onClosePopup);
			bgd.addEventListener(MouseEvent.CLICK, onClosePopup);
			
			closeBtn.buttonMode = closeBtn.useHandCursor = true;
			
		}
		
		
		private function removeEvents():void
		{
			closeBtn.buttonMode = closeBtn.useHandCursor = false;
			bgd.removeEventListener(MouseEvent.CLICK, onClosePopup);
			
			for (var i:int = 0; i < _dataItems.length; i++) 
			{
				_btns[i].removeEventListener(MouseEvent.CLICK, onCLickBtn);
			}
		}
		
		override public function onDestroy():void 
		{
			// чистим что тут есть и вызываем супера
			removeEvents();
			
			
			super.onDestroy();
		};
		
		override public function get data():Object { return _data; };
		
		override public function set data(value:Object):void 
		{
			super.data = value;
			
			trace('data', _data.data);
			
			//trace('data popup',_data.data[0].image);
			//trace('data popup',_data.currentId);
			
			headMarker.x = 107;
			headMarker.y = 80;
			setParamHead(HeadMarkerBase.BGD_RED);
			
			
			var nameBtn:Array = new Array();
			
			_dataItems = new Array();
			
			for each(var object:* in _data.data.cat)
			{
				var str:String = '';
				for each(var object1:* in object.head)
				{
					str = String(object1.text).substr(1);
				}
				//trace('head', str);
				nameBtn.push(str);
				
				var arr:Array = new Array();
				
				for each(var objectRegion:* in object.region)
				{
					var objRegion:Object = new Object();
					objRegion.posx = objectRegion.text[1];
					objRegion.posy = objectRegion.text[2];
					objRegion.name = objectRegion.text[3];
					objRegion.text = objectRegion.text[4];
					objRegion.image = (objectRegion.image[1].image_path != '') ? RemoteProxy.DOMEN + objectRegion.image[1].image_path : '';
					
					
					//trace('image',objectRegion.image[1].image_path);
					//trace('text1',objectRegion.text[1]);
					//trace('text2',objectRegion.text[2]);
					//trace('text3',objectRegion.text[3]);
					//trace('text4',objectRegion.text[4]);
					
					
					//for (var name:String in objectRegion.image[1]) 
					//{
						//trace('name=',name);
					//}
					//trace('---------------',objRegion.posx,objRegion.posy);
					
					arr.push(objRegion);
				}
				
				_dataItems.push({data:arr});
			}
			
			//trace('***',nameBtn.length);
			for (var i:int = 0; i < nameBtn.length; i++) 
			{
				//trace('+++', nameBtn[i]);
				
				if (nameBtn[i]!='') 
				{
					_btns[i].index.text = String(i+1);
					_btns[i].text.text = nameBtn[i];
					_btns[i].visible = true;
					_btns[i].id = i;
					_btns[i].addEventListener(MouseEvent.CLICK, onCLickBtn);
				}
				
				//_btn[i].text.text = nameBtn[i];
			}
			
			
			setEvents();
			
		};
		
		
		private function onClosePopup(e:MouseEvent):void 
		{
			trace('onClosePopup');
			removeEvents();
			
			var ev:UIEvent = new UIEvent(UIEvent.POPUP_CLOSE);
			dispatchEvent(ev);
			
		}
		
		private function onCLickBtn(e:MouseEvent):void 
		{
			//trace('id ',SelectableButton(e.target).id);
			
			removeEvents();
			
			
			// пока сделал так
			var ev:UIEvent = new UIEvent(UIEvent.POPUP_CLOSE);
			ev.datapopup = _dataItems[SelectableButton(e.target).id].data;
			dispatchEvent(ev);
			
		}
	}

}